<template>
  <div class="login-container">
    <el-card class="login-card">
      <h2 class="login-title">系统登录</h2>
      <el-form 
        ref="loginFormRef"  
        :model="loginForm" 
        :rules="loginRules" 
        @keyup.enter="handleLogin"
      >
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            placeholder="用户名"
            prefix-icon="User"
          />
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            placeholder="密码"
            prefix-icon="Lock"
            show-password
          />
        </el-form-item>
        <el-form-item>
          <el-button
            class="login-button"
            type="primary"
            :loading="loading"
            @click="handleLogin"
          >
            登录
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'

const router = useRouter()

const loginForm = ref({
  username: '',
  password: ''
})

const loginRules = ref({
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' }
  ]
})

const loading = ref(false)
const loginFormRef = ref(null)  // 定义表单引用

const handleLogin = () => {
  loginFormRef.value.validate(valid => {  // 使用正确的引用
    if (valid) {
      loading.value = true
      setTimeout(() => {
        if (loginForm.value.username === 'admin' && loginForm.value.password === '123456') {
          localStorage.setItem('isAuthenticated', 'true')
          ElMessage.success('登录成功')
          router.push('/home')
        } else {
          ElMessage.error('用户名或密码错误')
        }
        loading.value = false
      }, 1000)
    }
  })
}
</script>

<style scoped>
/* 样式部分保持不变 */
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
}

.login-card {
  width: 400px;
  padding: 30px;
  border-radius: 10px;
  box-shadow: 0 0 20px rgba(0, 0, 0, 0.1);
  background-color: rgba(255, 255, 255, 0.9);
}

.login-title {
  text-align: center;
  margin-bottom: 30px;
  color: #8a2be2; /* 粉紫色 */
}

.login-button {
  width: 100%;
  background-color: #8a2be2;
  border-color: #8a2be2;
}

.login-button:hover {
  background-color: #9932cc;
  border-color: #9932cc;
}

:deep(.el-input__wrapper) {
  border-radius: 5px;
}

:deep(.el-input__inner) {
  padding-left: 10px;
}
</style>